//
//  TestSliderComponent.m
//  MKUITestApp
//
//  Created by 苏尚进 on 2025/7/11.
//  Copyright © 2025 moon. All rights reserved.
//

#import "TestSliderComponent.h"

@interface TestSliderComponent_UISlider : UISlider
@end
@implementation TestSliderComponent_UISlider
- (CGSize)sizeThatFits:(CGSize)size{
    CGSize s = [super sizeThatFits:size];
    s.width = MIN(size.width,150);
    return s;
}
@end

@interface TestSliderComponent()
@property(nonatomic,strong) MKUIFlowLayoutConstraint *flowlayout;
@end

@implementation TestSliderComponent
- (id)initWithTitle:(NSString *)title value:(CGFloat)value minimumValue:(CGFloat)minimumValue maximumValue:(CGFloat)maximumValue whenChange:(void(^)(TestSliderComponent *el,CGFloat value))whenChange{
    if(self=[self init]){
        self.title = title;
        self.maximumValue = maximumValue;
        self.minimumValue = minimumValue;
        self.value = value;
        self.whenChange = whenChange;
    }
    return self;
}
- (id)initWithFrame:(CGRect)frame{
    if(self=[super initWithFrame:frame]){
        self.titleLabel = [[UILabel alloc] init];
        self.titleLabel.font = [UIFont systemFontOfSize:12];
        self.titleLabel.textColor = [UIColor mk_colorWithLight:[UIColor mk_colorWithString:@"#838383"]];
        [self addSubview:self.titleLabel];
        //
        self.valueLabel = [[UILabel alloc] init];
        self.valueLabel.font = [UIFont systemFontOfSize:12];
        self.valueLabel.textColor = [UIColor mk_colorWithLight:[UIColor mk_colorWithString:@"#838383"]];
        [self addSubview:self.valueLabel];
        //
        self.slider = [[TestSliderComponent_UISlider alloc] init];
        [self.slider addTarget:self action:@selector(_onSliderValueChange:) forControlEvents:(UIControlEventValueChanged)];
        [self addSubview:self.slider];
        //
        self.flowlayout = [[MKUIFlowLayoutConstraint alloc] initWithItems:@[self.titleLabel,self.valueLabel,self.slider] constraintParam:(MKUIFlowLayoutConstraintParam_H_C_R) contentInsets:UIEdgeInsetsZero interitemSpacing:2];
    }
    return self;
}
- (void)layoutSubviews{
    [super layoutSubviews];
    self.flowlayout.bounds = self.bounds;
    [self.flowlayout layoutItemsWithResizeItems:YES];
}
- (CGSize)sizeThatFits:(CGSize)size{
    return [self.flowlayout sizeThatFits:size resizeItems:YES];
}
- (void)setTitle:(NSString *)title{
    _title = title;
    self.titleLabel.text = title;
    [self setNeedsLayout];
}
- (void)setValue:(CGFloat)value{
    if(self.slider.value==value)return;
    self.slider.value = value;
    [self reloadData];
}
- (CGFloat)value{
    return self.slider.value;
}
- (void)setMinimumValue:(CGFloat)minimumValue{
    _minimumValue = minimumValue;
    self.slider.minimumValue = minimumValue;
}
- (void)setMaximumValue:(CGFloat)maximumValue{
    _maximumValue = maximumValue;
    self.slider.maximumValue = maximumValue;
}
- (void)reloadData{
    self.valueLabel.text = [NSString stringWithFormat:@"%.2f",self.value];
    [self setNeedsLayout];
}
- (void)_onSliderValueChange:(id)sender{
    [self reloadData];
    if(self.whenChange){
        self.whenChange(self, self.value);
    }
}
@end
